
#### ANALYSES ARE CONSISTENT WITH THE PRE-REGISTRATION (aspredicted #19604)
#### EXPLORATORY ANALYSES ARE CLEARLY IDENTIFIED 

#### LOAD PACKAGES AND SCRIPTS ####

library(boot)
library(dplyr)
library(lavaan)
library(lmtest) # tests assumptions of OLS
library(multicon) # relationships among multivariate constructs i.e. splithalf reliabilities
library(olsrr) # measure of collinearity for OLS regressions
library(psych) # basic psych package
library(RSA) # plotting of response surfaces

#### LOAD AND PREPARE DATA ####

#### set working directory here using setwd command
setwd("/Users/stephanecote/Dropbox/Research/Projects/Emotions in Organizations/Accuracy of Self-Insight/Follow-Up Study/Data/")

#### load data
selfinsight.temp1 <- read.csv("Côté and He - Self-Enhancement - Data.csv", header=TRUE, stringsAsFactors = FALSE)

#### REMOVE PARTICIPANTS WHO DO NOT MEET PRE-REGISTERED CRITERIA FOR INCLUSION IN ANALYSES ####

# count number of emotional abilities items attempted
selfinsight.temp1$emoitem1attempted <- NA; selfinsight.temp1$emoitem1attempted[selfinsight.temp1$eraitem1>0] <- 1;
selfinsight.temp1$emoitem2attempted <- NA; selfinsight.temp1$emoitem2attempted[selfinsight.temp1$eraitem2>0] <- 1;
selfinsight.temp1$emoitem3attempted <- NA; selfinsight.temp1$emoitem3attempted[selfinsight.temp1$eraitem3>0] <- 1;
selfinsight.temp1$emoitem4attempted <- NA; selfinsight.temp1$emoitem4attempted[selfinsight.temp1$eraitem4>0] <- 1;
selfinsight.temp1$emoitem5attempted <- NA; selfinsight.temp1$emoitem5attempted[selfinsight.temp1$eraitem5>0] <- 1;
selfinsight.temp1$emoitem6attempted <- NA; selfinsight.temp1$emoitem6attempted[selfinsight.temp1$eraitem6>0] <- 1;
selfinsight.temp1$emoitem7attempted <- NA; selfinsight.temp1$emoitem7attempted[selfinsight.temp1$eraitem7>0] <- 1;
selfinsight.temp1$emoitem8attempted <- NA; selfinsight.temp1$emoitem8attempted[selfinsight.temp1$eraitem8>0] <- 1;
selfinsight.temp1$emoitem9attempted <- NA; selfinsight.temp1$emoitem9attempted[selfinsight.temp1$eraitem9>0] <- 1;
selfinsight.temp1$emoitem10attempted <- NA; selfinsight.temp1$emoitem10attempted[selfinsight.temp1$eraitem10>0] <- 1;
selfinsight.temp1$emoitem11attempted <- NA; selfinsight.temp1$emoitem11attempted[selfinsight.temp1$eraitem11>0] <- 1;
selfinsight.temp1$emoitem12attempted <- NA; selfinsight.temp1$emoitem12attempted[selfinsight.temp1$eraitem12>0] <- 1;
selfinsight.temp1$emoitem13attempted <- NA; selfinsight.temp1$emoitem13attempted[selfinsight.temp1$eraitem13>0] <- 1;
selfinsight.temp1$emoitem14attempted <- NA; selfinsight.temp1$emoitem14attempted[selfinsight.temp1$eraitem14>0] <- 1;
selfinsight.temp1$emoitem15attempted <- NA; selfinsight.temp1$emoitem15attempted[selfinsight.temp1$eraitem15>0] <- 1;
selfinsight.temp1$emoitem16attempted <- NA; selfinsight.temp1$emoitem16attempted[selfinsight.temp1$eraitem16>0] <- 1;
selfinsight.temp1$emoitem17attempted <- NA; selfinsight.temp1$emoitem17attempted[selfinsight.temp1$eraitem17>0] <- 1;
selfinsight.temp1$emoitem18attempted <- NA; selfinsight.temp1$emoitem18attempted[selfinsight.temp1$eraitem18>0] <- 1;
selfinsight.temp1$emoitem19attempted <- NA; selfinsight.temp1$emoitem19attempted[selfinsight.temp1$eraitem19>0] <- 1;
selfinsight.temp1$emoitem20attempted <- NA; selfinsight.temp1$emoitem20attempted[selfinsight.temp1$eraitem20>0] <- 1;
selfinsight.temp1$emoitem21attempted <- NA; selfinsight.temp1$emoitem21attempted[selfinsight.temp1$eraitem21>0] <- 1;
selfinsight.temp1$emoitem22attempted <- NA; selfinsight.temp1$emoitem22attempted[selfinsight.temp1$eraitem22>0] <- 1;
selfinsight.temp1$emoitem23attempted <- NA; selfinsight.temp1$emoitem23attempted[selfinsight.temp1$eraitem23>0] <- 1;
selfinsight.temp1$emoitem24attempted <- NA; selfinsight.temp1$emoitem24attempted[selfinsight.temp1$eraitem24>0] <- 1;
selfinsight.temp1$emoitem25attempted <- NA; selfinsight.temp1$emoitem25attempted[selfinsight.temp1$eraitem25>0] <- 1;
selfinsight.temp1$emoitem26attempted <- NA; selfinsight.temp1$emoitem26attempted[selfinsight.temp1$eraitem26>0] <- 1;
selfinsight.temp1$emoitem27attempted <- NA; selfinsight.temp1$emoitem27attempted[selfinsight.temp1$eraitem27>0] <- 1;
selfinsight.temp1$emoitem28attempted <- NA; selfinsight.temp1$emoitem28attempted[selfinsight.temp1$eraitem28>0] <- 1;
selfinsight.temp1$emoitem29attempted <- NA; selfinsight.temp1$emoitem29attempted[selfinsight.temp1$eraitem29>0] <- 1;
selfinsight.temp1$emoitem30attempted <- NA; selfinsight.temp1$emoitem30attempted[selfinsight.temp1$eraitem30>0] <- 1;
selfinsight.temp1$emoitem31attempted <- NA; selfinsight.temp1$emoitem31attempted[selfinsight.temp1$eraitem31>0] <- 1;
selfinsight.temp1$emoitem32attempted <- NA; selfinsight.temp1$emoitem32attempted[selfinsight.temp1$eraitem32>0] <- 1;
selfinsight.temp1$emoitem33attempted <- NA; selfinsight.temp1$emoitem33attempted[selfinsight.temp1$eraitem33>0] <- 1;
selfinsight.temp1$emoitem34attempted <- NA; selfinsight.temp1$emoitem34attempted[selfinsight.temp1$eraitem34>0] <- 1;
selfinsight.temp1$emoitem35attempted <- NA; selfinsight.temp1$emoitem35attempted[selfinsight.temp1$eraitem35>0] <- 1;
selfinsight.temp1$emoitem36attempted <- NA; selfinsight.temp1$emoitem36attempted[selfinsight.temp1$eraitem36>0] <- 1;
selfinsight.temp1$emoitem37attempted <- NA; selfinsight.temp1$emoitem37attempted[selfinsight.temp1$eraitem37>0] <- 1;
selfinsight.temp1$emoitem38attempted <- NA; selfinsight.temp1$emoitem38attempted[selfinsight.temp1$eraitem38>0] <- 1;
selfinsight.temp1$emoitem39attempted <- NA; selfinsight.temp1$emoitem39attempted[selfinsight.temp1$eraitem39>0] <- 1;
selfinsight.temp1$emoitem40attempted <- NA; selfinsight.temp1$emoitem40attempted[selfinsight.temp1$eraitem40>0] <- 1;
selfinsight.temp1$emoitem41attempted <- NA; selfinsight.temp1$emoitem41attempted[selfinsight.temp1$eraitem41>0] <- 1;
selfinsight.temp1$emoitem42attempted <- NA; selfinsight.temp1$emoitem42attempted[selfinsight.temp1$eraitem42>0] <- 1;
selfinsight.temp1$emoitem43attempted <- NA; selfinsight.temp1$emoitem43attempted[selfinsight.temp1$eraitem43>0] <- 1;
selfinsight.temp1$emoitem44attempted <- NA; selfinsight.temp1$emoitem44attempted[selfinsight.temp1$eraitem44>0] <- 1;
selfinsight.temp1$emoitem45attempted <- NA; selfinsight.temp1$emoitem45attempted[selfinsight.temp1$eraitem45>0] <- 1;
selfinsight.temp1$emoitem46attempted <- NA; selfinsight.temp1$emoitem46attempted[selfinsight.temp1$eraitem46>0] <- 1;
selfinsight.temp1$emoitem47attempted <- NA; selfinsight.temp1$emoitem47attempted[selfinsight.temp1$eraitem47>0] <- 1;
selfinsight.temp1$emoitem48attempted <- NA; selfinsight.temp1$emoitem48attempted[selfinsight.temp1$eraitem48>0] <- 1;
selfinsight.temp1$emoitem49attempted <- NA; selfinsight.temp1$emoitem49attempted[selfinsight.temp1$eraitem49>0] <- 1;
selfinsight.temp1$emoitem50attempted <- NA; selfinsight.temp1$emoitem50attempted[selfinsight.temp1$eraitem50>0] <- 1;
selfinsight.temp1$emoitem51attempted <- NA; selfinsight.temp1$emoitem51attempted[selfinsight.temp1$eraitem51>0] <- 1;
selfinsight.temp1$emoitem52attempted <- NA; selfinsight.temp1$emoitem52attempted[selfinsight.temp1$eraitem52>0] <- 1;
selfinsight.temp1$emoitem53attempted <- NA; selfinsight.temp1$emoitem53attempted[selfinsight.temp1$eraitem53>0] <- 1;
selfinsight.temp1$emoitem54attempted <- NA; selfinsight.temp1$emoitem54attempted[selfinsight.temp1$eraitem54>0] <- 1;
selfinsight.temp1$emoitem55attempted <- NA; selfinsight.temp1$emoitem55attempted[selfinsight.temp1$eraitem55>0] <- 1;
selfinsight.temp1$emoitem56attempted <- NA; selfinsight.temp1$emoitem56attempted[selfinsight.temp1$eraitem56>0] <- 1;
selfinsight.temp1$emoitem57attempted <- NA; selfinsight.temp1$emoitem57attempted[selfinsight.temp1$eraitem57>0] <- 1;
selfinsight.temp1$emoitem58attempted <- NA; selfinsight.temp1$emoitem58attempted[selfinsight.temp1$eraitem58>0] <- 1;
selfinsight.temp1$emoitem59attempted <- NA; selfinsight.temp1$emoitem59attempted[selfinsight.temp1$eraitem59>0] <- 1;
selfinsight.temp1$emoitem60attempted <- NA; selfinsight.temp1$emoitem60attempted[selfinsight.temp1$eraitem60>0] <- 1;
selfinsight.temp1$emoitem61attempted <- NA; selfinsight.temp1$emoitem61attempted[selfinsight.temp1$eraitem61>0] <- 1;
selfinsight.temp1$emoitem62attempted <- NA; selfinsight.temp1$emoitem62attempted[selfinsight.temp1$eraitem62>0] <- 1;
selfinsight.temp1$emoitem63attempted <- NA; selfinsight.temp1$emoitem63attempted[selfinsight.temp1$eraitem63>0] <- 1;
selfinsight.temp1$emoitem64attempted <- NA; selfinsight.temp1$emoitem64attempted[selfinsight.temp1$eraitem64>0] <- 1;
selfinsight.temp1$emoitem65attempted <- NA; selfinsight.temp1$emoitem65attempted[selfinsight.temp1$eraitem65>0] <- 1;
selfinsight.temp1$emoitem66attempted <- NA; selfinsight.temp1$emoitem66attempted[selfinsight.temp1$eraitem66>0] <- 1;
selfinsight.temp1$emoitem67attempted <- NA; selfinsight.temp1$emoitem67attempted[selfinsight.temp1$eraitem67>0] <- 1;
selfinsight.temp1$emoitem68attempted <- NA; selfinsight.temp1$emoitem68attempted[selfinsight.temp1$eraitem68>0] <- 1;
selfinsight.temp1$emoitem69attempted <- NA; selfinsight.temp1$emoitem69attempted[selfinsight.temp1$eraitem69>0] <- 1;
selfinsight.temp1$emoitem70attempted <- NA; selfinsight.temp1$emoitem70attempted[selfinsight.temp1$eraitem70>0] <- 1;
selfinsight.temp1$emoitem71attempted <- NA; selfinsight.temp1$emoitem71attempted[selfinsight.temp1$eraitem71>0] <- 1;
selfinsight.temp1$emoitem72attempted <- NA; selfinsight.temp1$emoitem72attempted[selfinsight.temp1$eraitem72>0] <- 1;
selfinsight.temp1$ERAtotalattempted<- with(selfinsight.temp1, (rowSums(cbind(emoitem1attempted,emoitem2attempted,emoitem3attempted,emoitem4attempted,emoitem5attempted,emoitem6attempted,emoitem7attempted,emoitem8attempted,emoitem9attempted,emoitem10attempted,emoitem11attempted,emoitem12attempted,emoitem13attempted,emoitem14attempted,emoitem15attempted,emoitem16attempted,emoitem17attempted,emoitem18attempted,emoitem19attempted,emoitem20attempted,emoitem21attempted,emoitem22attempted,emoitem23attempted,emoitem24attempted,emoitem25attempted,emoitem26attempted,emoitem27attempted,emoitem28attempted,emoitem29attempted,emoitem30attempted,emoitem31attempted,emoitem32attempted,emoitem33attempted,emoitem34attempted,emoitem35attempted,emoitem36attempted,emoitem37attempted,emoitem38attempted,emoitem39attempted,emoitem40attempted,emoitem41attempted,emoitem42attempted,emoitem43attempted,emoitem44attempted,emoitem45attempted,emoitem46attempted,emoitem47attempted,emoitem48attempted,emoitem49attempted,emoitem50attempted,emoitem51attempted,emoitem52attempted,emoitem53attempted,emoitem54attempted,emoitem55attempted,emoitem56attempted,emoitem57attempted,emoitem58attempted,emoitem59attempted,emoitem60attempted,emoitem61attempted,emoitem62attempted,emoitem63attempted,emoitem64attempted,emoitem65attempted,emoitem66attempted,emoitem67attempted,emoitem68attempted,emoitem69attempted,emoitem70attempted,emoitem71attempted,emoitem72attempted), na.rm=TRUE)))
table(selfinsight.temp1$ERAtotalattempted)

# compare answer on first item to answer on all other items to see if participant chose the same letter for all items
selfinsight.temp1$emocomp2 <- NA; selfinsight.temp1$emocomp2[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem2] <- 1; 
selfinsight.temp1$emocomp3 <- NA; selfinsight.temp1$emocomp3[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem3] <- 1; 
selfinsight.temp1$emocomp4 <- NA; selfinsight.temp1$emocomp4[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem4] <- 1; 
selfinsight.temp1$emocomp5 <- NA; selfinsight.temp1$emocomp5[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem5] <- 1; 
selfinsight.temp1$emocomp6 <- NA; selfinsight.temp1$emocomp6[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem6] <- 1; 
selfinsight.temp1$emocomp7 <- NA; selfinsight.temp1$emocomp7[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem7] <- 1; 
selfinsight.temp1$emocomp8 <- NA; selfinsight.temp1$emocomp8[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem8] <- 1; 
selfinsight.temp1$emocomp9 <- NA; selfinsight.temp1$emocomp9[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem9] <- 1; 
selfinsight.temp1$emocomp10 <- NA; selfinsight.temp1$emocomp10[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem10] <- 1; 
selfinsight.temp1$emocomp11 <- NA; selfinsight.temp1$emocomp11[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem11] <- 1; 
selfinsight.temp1$emocomp12 <- NA; selfinsight.temp1$emocomp12[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem12] <- 1; 
selfinsight.temp1$emocomp13 <- NA; selfinsight.temp1$emocomp13[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem13] <- 1; 
selfinsight.temp1$emocomp14 <- NA; selfinsight.temp1$emocomp14[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem14] <- 1; 
selfinsight.temp1$emocomp15 <- NA; selfinsight.temp1$emocomp15[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem15] <- 1; 
selfinsight.temp1$emocomp16 <- NA; selfinsight.temp1$emocomp16[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem16] <- 1; 
selfinsight.temp1$emocomp17 <- NA; selfinsight.temp1$emocomp17[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem17] <- 1; 
selfinsight.temp1$emocomp18 <- NA; selfinsight.temp1$emocomp18[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem18] <- 1; 
selfinsight.temp1$emocomp19 <- NA; selfinsight.temp1$emocomp19[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem19] <- 1; 
selfinsight.temp1$emocomp20 <- NA; selfinsight.temp1$emocomp20[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem20] <- 1; 
selfinsight.temp1$emocomp21 <- NA; selfinsight.temp1$emocomp21[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem21] <- 1; 
selfinsight.temp1$emocomp22 <- NA; selfinsight.temp1$emocomp22[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem22] <- 1; 
selfinsight.temp1$emocomp23 <- NA; selfinsight.temp1$emocomp23[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem23] <- 1; 
selfinsight.temp1$emocomp24 <- NA; selfinsight.temp1$emocomp24[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem24] <- 1; 
selfinsight.temp1$emocomp25 <- NA; selfinsight.temp1$emocomp25[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem25] <- 1; 
selfinsight.temp1$emocomp26 <- NA; selfinsight.temp1$emocomp26[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem26] <- 1; 
selfinsight.temp1$emocomp27 <- NA; selfinsight.temp1$emocomp27[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem27] <- 1; 
selfinsight.temp1$emocomp28 <- NA; selfinsight.temp1$emocomp28[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem28] <- 1; 
selfinsight.temp1$emocomp29 <- NA; selfinsight.temp1$emocomp29[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem29] <- 1; 
selfinsight.temp1$emocomp30 <- NA; selfinsight.temp1$emocomp30[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem30] <- 1; 
selfinsight.temp1$emocomp31 <- NA; selfinsight.temp1$emocomp31[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem31] <- 1; 
selfinsight.temp1$emocomp32 <- NA; selfinsight.temp1$emocomp32[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem32] <- 1; 
selfinsight.temp1$emocomp33 <- NA; selfinsight.temp1$emocomp33[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem33] <- 1; 
selfinsight.temp1$emocomp34 <- NA; selfinsight.temp1$emocomp34[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem34] <- 1; 
selfinsight.temp1$emocomp35 <- NA; selfinsight.temp1$emocomp35[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem35] <- 1; 
selfinsight.temp1$emocomp36 <- NA; selfinsight.temp1$emocomp36[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem36] <- 1; 
selfinsight.temp1$emocomp37 <- NA; selfinsight.temp1$emocomp37[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem37] <- 1; 
selfinsight.temp1$emocomp38 <- NA; selfinsight.temp1$emocomp38[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem38] <- 1; 
selfinsight.temp1$emocomp39 <- NA; selfinsight.temp1$emocomp39[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem39] <- 1; 
selfinsight.temp1$emocomp40 <- NA; selfinsight.temp1$emocomp40[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem40] <- 1; 
selfinsight.temp1$emocomp41 <- NA; selfinsight.temp1$emocomp41[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem41] <- 1; 
selfinsight.temp1$emocomp42 <- NA; selfinsight.temp1$emocomp42[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem42] <- 1; 
selfinsight.temp1$emocomp43 <- NA; selfinsight.temp1$emocomp43[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem43] <- 1; 
selfinsight.temp1$emocomp44 <- NA; selfinsight.temp1$emocomp44[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem44] <- 1; 
selfinsight.temp1$emocomp45 <- NA; selfinsight.temp1$emocomp45[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem45] <- 1; 
selfinsight.temp1$emocomp46 <- NA; selfinsight.temp1$emocomp46[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem46] <- 1; 
selfinsight.temp1$emocomp47 <- NA; selfinsight.temp1$emocomp47[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem47] <- 1; 
selfinsight.temp1$emocomp48 <- NA; selfinsight.temp1$emocomp48[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem48] <- 1; 
selfinsight.temp1$emocomp49 <- NA; selfinsight.temp1$emocomp49[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem49] <- 1; 
selfinsight.temp1$emocomp50 <- NA; selfinsight.temp1$emocomp50[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem50] <- 1; 
selfinsight.temp1$emocomp51 <- NA; selfinsight.temp1$emocomp51[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem51] <- 1; 
selfinsight.temp1$emocomp52 <- NA; selfinsight.temp1$emocomp52[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem52] <- 1; 
selfinsight.temp1$emocomp53 <- NA; selfinsight.temp1$emocomp53[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem53] <- 1; 
selfinsight.temp1$emocomp54 <- NA; selfinsight.temp1$emocomp54[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem54] <- 1; 
selfinsight.temp1$emocomp55 <- NA; selfinsight.temp1$emocomp55[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem55] <- 1; 
selfinsight.temp1$emocomp56 <- NA; selfinsight.temp1$emocomp56[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem56] <- 1; 
selfinsight.temp1$emocomp57 <- NA; selfinsight.temp1$emocomp57[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem57] <- 1; 
selfinsight.temp1$emocomp58 <- NA; selfinsight.temp1$emocomp58[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem58] <- 1; 
selfinsight.temp1$emocomp59 <- NA; selfinsight.temp1$emocomp59[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem59] <- 1; 
selfinsight.temp1$emocomp60 <- NA; selfinsight.temp1$emocomp60[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem60] <- 1; 
selfinsight.temp1$emocomp61 <- NA; selfinsight.temp1$emocomp61[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem61] <- 1; 
selfinsight.temp1$emocomp62 <- NA; selfinsight.temp1$emocomp62[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem62] <- 1; 
selfinsight.temp1$emocomp63 <- NA; selfinsight.temp1$emocomp63[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem63] <- 1; 
selfinsight.temp1$emocomp64 <- NA; selfinsight.temp1$emocomp64[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem64] <- 1; 
selfinsight.temp1$emocomp65 <- NA; selfinsight.temp1$emocomp65[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem65] <- 1; 
selfinsight.temp1$emocomp66 <- NA; selfinsight.temp1$emocomp66[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem66] <- 1; 
selfinsight.temp1$emocomp67 <- NA; selfinsight.temp1$emocomp67[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem67] <- 1; 
selfinsight.temp1$emocomp68 <- NA; selfinsight.temp1$emocomp68[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem68] <- 1; 
selfinsight.temp1$emocomp69 <- NA; selfinsight.temp1$emocomp69[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem69] <- 1; 
selfinsight.temp1$emocomp70 <- NA; selfinsight.temp1$emocomp70[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem70] <- 1; 
selfinsight.temp1$emocomp71 <- NA; selfinsight.temp1$emocomp71[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem71] <- 1; 
selfinsight.temp1$emocomp72 <- NA; selfinsight.temp1$emocomp72[selfinsight.temp1$eraitem1==selfinsight.temp1$eraitem72] <- 1; 
selfinsight.temp1$totalequalemocomp <- with(selfinsight.temp1,(rowSums(cbind(emocomp2,emocomp3,emocomp4,emocomp5,emocomp6,emocomp7,emocomp8,emocomp9,emocomp10,emocomp11,emocomp12,emocomp13,emocomp14,emocomp15,emocomp16,emocomp17,emocomp18,emocomp19,emocomp20,emocomp21,emocomp22,emocomp23,emocomp24,emocomp25,emocomp26,emocomp27,emocomp28,emocomp29,emocomp30,emocomp31,emocomp32,emocomp33,emocomp34,emocomp35,emocomp36,emocomp37,emocomp38,emocomp39,emocomp40,emocomp41,emocomp42,emocomp43,emocomp44,emocomp45,emocomp46,emocomp47,emocomp48,emocomp49,emocomp50,emocomp51,emocomp52,emocomp53,emocomp54,emocomp55,emocomp56,emocomp57,emocomp58,emocomp59,emocomp60,emocomp61,emocomp62,emocomp63,emocomp64,emocomp65,emocomp66,emocomp67,emocomp68,emocomp69,emocomp70,emocomp71,emocomp72), na.rm=TRUE)))
table(selfinsight.temp1$totalequalemocomp)
# note: if the total of equal comparisons is 71 then the participant chose the same letter for all items and should be excluded

selfinsight.temp1$ertselfrating<- as.numeric(selfinsight.temp1$ertselfrating)
describe(selfinsight.temp1$ertselfrating)
table(selfinsight.temp1$ertselfrating)

# identify if emotional abilities data is valid (i.e., at least half of the items are attempted, the same letter was not picked for all items, and there is a self-perception)
selfinsight.temp1$emotionalabilitiesvalid <- NA; selfinsight.temp1$emotionalabilitiesvalid[selfinsight.temp1$ERAtotalattempted>35 & selfinsight.temp1$totalequalemocomp!=71 & selfinsight.temp1$ertselfrating>-1] <- 1
table(selfinsight.temp1$emotionalabilitiesvalid)

# count number of cognitive abilities items attempted #
selfinsight.temp1$cogitem1attempted <- NA; selfinsight.temp1$cogitem1attempted[selfinsight.temp1$Raven1>0] <- 1;
selfinsight.temp1$cogitem2attempted <- NA; selfinsight.temp1$cogitem2attempted[selfinsight.temp1$Raven2>0] <- 1;
selfinsight.temp1$cogitem3attempted <- NA; selfinsight.temp1$cogitem3attempted[selfinsight.temp1$Raven3>0] <- 1;
selfinsight.temp1$cogitem4attempted <- NA; selfinsight.temp1$cogitem4attempted[selfinsight.temp1$Raven4>0] <- 1;
selfinsight.temp1$cogitem5attempted <- NA; selfinsight.temp1$cogitem5attempted[selfinsight.temp1$Raven5>0] <- 1;
selfinsight.temp1$cogitem6attempted <- NA; selfinsight.temp1$cogitem6attempted[selfinsight.temp1$Raven6>0] <- 1;
selfinsight.temp1$cogitem7attempted <- NA; selfinsight.temp1$cogitem7attempted[selfinsight.temp1$Raven7>0] <- 1;
selfinsight.temp1$cogitem8attempted <- NA; selfinsight.temp1$cogitem8attempted[selfinsight.temp1$Raven8>0] <- 1;
selfinsight.temp1$cogitem9attempted <- NA; selfinsight.temp1$cogitem9attempted[selfinsight.temp1$Raven9>0] <- 1;
selfinsight.temp1$cogitem10attempted <- NA; selfinsight.temp1$cogitem10attempted[selfinsight.temp1$Raven10>0] <- 1;
selfinsight.temp1$cogitem11attempted <- NA; selfinsight.temp1$cogitem11attempted[selfinsight.temp1$Raven11>0] <- 1;
selfinsight.temp1$cogitem12attempted <- NA; selfinsight.temp1$cogitem12attempted[selfinsight.temp1$Raven12>0] <- 1;
selfinsight.temp1$cogitem13attempted <- NA; selfinsight.temp1$cogitem13attempted[selfinsight.temp1$Raven13>0] <- 1;
selfinsight.temp1$cogitem14attempted <- NA; selfinsight.temp1$cogitem14attempted[selfinsight.temp1$Raven14>0] <- 1;
selfinsight.temp1$cogitem15attempted <- NA; selfinsight.temp1$cogitem15attempted[selfinsight.temp1$Raven15>0] <- 1;
selfinsight.temp1$raventotalattempted<- with(selfinsight.temp1, (rowSums(cbind(cogitem1attempted,cogitem2attempted,cogitem3attempted,cogitem4attempted,cogitem5attempted,cogitem6attempted,cogitem7attempted,cogitem8attempted,cogitem9attempted,cogitem10attempted,cogitem11attempted,cogitem12attempted,cogitem13attempted,cogitem14attempted,cogitem15attempted), na.rm=TRUE)))
table(selfinsight.temp1$raventotalattempted)

# compare answer on first item to answer on all other items to see if participant chose the same letter for all items
selfinsight.temp1$cogcomp2 <- NA; selfinsight.temp1$cogcomp2[selfinsight.temp1$Raven1==selfinsight.temp1$Raven2] <- 1; 
selfinsight.temp1$cogcomp3 <- NA; selfinsight.temp1$cogcomp3[selfinsight.temp1$Raven1==selfinsight.temp1$Raven3] <- 1; 
selfinsight.temp1$cogcomp4 <- NA; selfinsight.temp1$cogcomp4[selfinsight.temp1$Raven1==selfinsight.temp1$Raven4] <- 1; 
selfinsight.temp1$cogcomp5 <- NA; selfinsight.temp1$cogcomp5[selfinsight.temp1$Raven1==selfinsight.temp1$Raven5] <- 1; 
selfinsight.temp1$cogcomp6 <- NA; selfinsight.temp1$cogcomp6[selfinsight.temp1$Raven1==selfinsight.temp1$Raven6] <- 1; 
selfinsight.temp1$cogcomp7 <- NA; selfinsight.temp1$cogcomp7[selfinsight.temp1$Raven1==selfinsight.temp1$Raven7] <- 1; 
selfinsight.temp1$cogcomp8 <- NA; selfinsight.temp1$cogcomp8[selfinsight.temp1$Raven1==selfinsight.temp1$Raven8] <- 1; 
selfinsight.temp1$cogcomp9 <- NA; selfinsight.temp1$cogcomp9[selfinsight.temp1$Raven1==selfinsight.temp1$Raven9] <- 1; 
selfinsight.temp1$cogcomp10 <- NA; selfinsight.temp1$cogcomp10[selfinsight.temp1$Raven1==selfinsight.temp1$Raven10] <- 1; 
selfinsight.temp1$cogcomp11 <- NA; selfinsight.temp1$cogcomp11[selfinsight.temp1$Raven1==selfinsight.temp1$Raven11] <- 1; 
selfinsight.temp1$cogcomp12 <- NA; selfinsight.temp1$cogcomp12[selfinsight.temp1$Raven1==selfinsight.temp1$Raven12] <- 1; 
selfinsight.temp1$cogcomp13 <- NA; selfinsight.temp1$cogcomp13[selfinsight.temp1$Raven1==selfinsight.temp1$Raven13] <- 1; 
selfinsight.temp1$cogcomp14 <- NA; selfinsight.temp1$cogcomp14[selfinsight.temp1$Raven1==selfinsight.temp1$Raven14] <- 1; 
selfinsight.temp1$cogcomp15 <- NA; selfinsight.temp1$cogcomp15[selfinsight.temp1$Raven1==selfinsight.temp1$Raven15] <- 1; 
selfinsight.temp1$totalequalcogcomp <- with(selfinsight.temp1,(rowSums(cbind(cogcomp2,cogcomp3,cogcomp4,cogcomp5,cogcomp6,cogcomp7,cogcomp8,cogcomp9,cogcomp10,cogcomp11,cogcomp12,cogcomp13,cogcomp14,cogcomp15), na.rm=TRUE)))
table(selfinsight.temp1$totalequalcogcomp)
# note: if the total of equal comparisons is 14 then the participant chose the same letter for all items and should be excluded

selfinsight.temp1$ravenselfrating<- as.numeric(selfinsight.temp1$ravenselfrating)
describe(selfinsight.temp1$ravenselfrating)
table(selfinsight.temp1$ravenselfrating)

# identify if cognitive abilities data is valid (i.e., at least half of the items are attempted, the same letter was not picked for all items, and there is a self-perception)
selfinsight.temp1$cognitiveabilitiesvalid <- NA; selfinsight.temp1$cognitiveabilitiesvalid[selfinsight.temp1$raventotalattempted>7 & selfinsight.temp1$totalequalcogcomp!=14 & selfinsight.temp1$ravenselfrating>-1] <- 1
table(selfinsight.temp1$cognitiveabilitiesvalid)

# identify if any adjustment measure completed

selfinsight.temp1$swls_1<- as.numeric(selfinsight.temp1$swls_1); selfinsight.temp1$swls_2<- as.numeric(selfinsight.temp1$swls_2); selfinsight.temp1$swls_3<- as.numeric(selfinsight.temp1$swls_3); selfinsight.temp1$swls_4<- as.numeric(selfinsight.temp1$swls_4); selfinsight.temp1$swls_5<- as.numeric(selfinsight.temp1$swls_5)
selfinsight.temp1$life.satisfaction <- with(selfinsight.temp1,(rowMeans(cbind(swls_1,swls_2,swls_3,swls_4,swls_5), na.rm=TRUE)))

selfinsight.temp1$institutional_1<- as.numeric(selfinsight.temp1$institutional_1); selfinsight.temp1$institutional_2<- as.numeric(selfinsight.temp1$institutional_2); selfinsight.temp1$institutional_3<- as.numeric(selfinsight.temp1$institutional_3); selfinsight.temp1$institutional_4<- as.numeric(selfinsight.temp1$institutional_4); selfinsight.temp1$institutional_5<- as.numeric(selfinsight.temp1$institutional_5)
selfinsight.temp1$career.satisfaction <- with(selfinsight.temp1,(rowMeans(cbind(institutional_1,institutional_2,institutional_3,institutional_4,institutional_5), na.rm=TRUE)))

selfinsight.temp1$interpersonal_1<- as.numeric(selfinsight.temp1$interpersonal_1); selfinsight.temp1$interpersonal_2<- as.numeric(selfinsight.temp1$interpersonal_2); selfinsight.temp1$interpersonal_3<- as.numeric(selfinsight.temp1$interpersonal_3); selfinsight.temp1$interpersonal_4<- as.numeric(selfinsight.temp1$interpersonal_4)
selfinsight.temp1$r.interpersonal_3 <- 6-selfinsight.temp1$interpersonal_3; selfinsight.temp1$r.interpersonal_4 <- 6-selfinsight.temp1$interpersonal_4
selfinsight.temp1$relationship.satisfaction <- with(selfinsight.temp1,(rowMeans(cbind(interpersonal_1,interpersonal_2,r.interpersonal_3,r.interpersonal_4), na.rm=TRUE)))

selfinsight.temp1$adjustmentvalid <- NA; selfinsight.temp1$adjustmentvalid[selfinsight.temp1$life.satisfaction>-1 & selfinsight.temp1$career.satisfaction>-1 & selfinsight.temp1$relationship.satisfaction>-1] <- 1
table(selfinsight.temp1$adjustmentvalid)

#### create the data frame (selfinsight) with only participants who meet the pre-registered criteria for inclusion

selfinsight.temp2 <- subset(selfinsight.temp1, adjustmentvalid==1)
selfinsight <- subset(selfinsight.temp2, emotionalabilitiesvalid==1 | cognitiveabilitiesvalid==1)

#### CODE DEMOGRAPHIC VARIABLES ####

# age
selfinsight$age<- as.numeric(selfinsight$age)
selfinsight$age.corrected <- selfinsight$age+12
describe(selfinsight$age.corrected)

# education
table(selfinsight$education)
#1 less than high school
#2 high school graduation or some university
#3 university graduation
#4 master's degree
#5 PhD degree
#6 Professional degree (MD or JD)
#7 Other (please specify)
table(selfinsight$education_TEXT)

# gender
table(selfinsight$gender)
# recode open-ended responses into gender categories
selfinsight$gendercoded <- NA;
selfinsight$gendercoded[selfinsight$gender=="m"|selfinsight$gender=="M"|selfinsight$gender=="male"| selfinsight$gender=="Male"|selfinsight$gender=="MALE"|selfinsight$gender=="Male "|selfinsight$gender=="male "|selfinsight$gender=="man"] <- 1;
selfinsight$gendercoded[selfinsight$gender=="f"| selfinsight$gender=="F"|selfinsight$gender=="female"|selfinsight$gender=="Female"|selfinsight$gender=="FEMALE"|selfinsight$gender=="Female "|selfinsight$gender=="femalee"|selfinsight$gender=="femals"|selfinsight$gender=="woman"|selfinsight$gender=="Woman"] <- 2;
selfinsight$gendercoded[selfinsight$gender=="Female, non binary"|selfinsight$gender=="Gender fluid"|selfinsight$gender=="Non-binary"|selfinsight$gender=="trans masculine"] <- 3;
table(selfinsight$gendercoded)
# male (1)
# female (2)
# other (3)

# ethnicity
# Caucasian  (1) 
# East Asian  (2) 
# Southeast Asian  (3) 
# West Indian  (4) 
# Middle Eastern  (5) 
# African American  (6) 
# Hispanic  (7) 
# Other (specify):  (8) 
table(selfinsight$ethnicity_1)
table(selfinsight$ethnicity_2)
table(selfinsight$ethnicity_3)
table(selfinsight$ethnicity_4)
table(selfinsight$ethnicity_5)
table(selfinsight$ethnicity_6)
table(selfinsight$ethnicity_7)
table(selfinsight$ethnicity_8)
table(selfinsight$ethnicity_8_TEXT)

#### CODE CANDIDATE MECHANISM MEASURES ####

selfinsight$thinkthesame_1<- as.numeric(selfinsight$thinkthesame_1); selfinsight$thinkthesame_2<- as.numeric(selfinsight$thinkthesame_2); selfinsight$thinkthesame_3<- as.numeric(selfinsight$thinkthesame_3)
selfinsight$compliance.norms <- with(selfinsight,(rowMeans(cbind(thinkthesame_1,thinkthesame_2,thinkthesame_3), na.rm=TRUE)))
with(selfinsight, psych::alpha(data.frame(thinkthesame_1,thinkthesame_2,thinkthesame_3), na.rm=TRUE, check.keys=FALSE))

selfinsight$thinkasshould_1<- as.numeric(selfinsight$thinkasshould_1); selfinsight$thinkasshould_2<- as.numeric(selfinsight$thinkasshould_2); selfinsight$thinkasshould_3<- as.numeric(selfinsight$thinkasshould_3)
selfinsight$adherence.guidance <- with(selfinsight,(rowMeans(cbind(thinkasshould_1,thinkasshould_2,thinkasshould_3), na.rm=TRUE)))
with(selfinsight, psych::alpha(data.frame(thinkasshould_1,thinkasshould_2,thinkasshould_3), na.rm=TRUE, check.keys=FALSE))

selfinsight$defensiveness1_1<- as.numeric(selfinsight$defensiveness1_1); selfinsight$defensiveness1_2<- as.numeric(selfinsight$defensiveness1_2); selfinsight$defensiveness1_3<- as.numeric(selfinsight$defensiveness1_3)
selfinsight$r.defensiveness1_1 <- 6-selfinsight$defensiveness1_1; selfinsight$std.r.defensiveness1_1 <- (selfinsight$r.defensiveness1_1-mean(selfinsight$r.defensiveness1_1,na.rm=TRUE))/sd(selfinsight$r.defensiveness1_1,na.rm=TRUE)
selfinsight$r.defensiveness1_2 <- 6-selfinsight$defensiveness1_2; selfinsight$std.r.defensiveness1_2 <- (selfinsight$r.defensiveness1_2-mean(selfinsight$r.defensiveness1_2,na.rm=TRUE))/sd(selfinsight$r.defensiveness1_2,na.rm=TRUE)
selfinsight$r.defensiveness1_3 <- 6-selfinsight$defensiveness1_3; selfinsight$std.r.defensiveness1_3 <- (selfinsight$r.defensiveness1_3-mean(selfinsight$r.defensiveness1_3,na.rm=TRUE))/sd(selfinsight$r.defensiveness1_3,na.rm=TRUE)
selfinsight$defensiveness2_1<- as.numeric(selfinsight$defensiveness2_1); selfinsight$defensiveness2_2<- as.numeric(selfinsight$defensiveness2_2); selfinsight$defensiveness2_3<- as.numeric(selfinsight$defensiveness2_3)
selfinsight$std.defensiveness2_1 <- (selfinsight$defensiveness2_1-mean(selfinsight$defensiveness2_1,na.rm=TRUE))/sd(selfinsight$defensiveness2_1,na.rm=TRUE)
selfinsight$r.defensiveness2_2 <- 8-selfinsight$defensiveness2_2; selfinsight$r.defensiveness2_3 <- 8-selfinsight$defensiveness2_3 
selfinsight$std.r.defensiveness2_2 <- (selfinsight$r.defensiveness2_2-mean(selfinsight$r.defensiveness2_2,na.rm=TRUE))/sd(selfinsight$r.defensiveness2_2,na.rm=TRUE)
selfinsight$std.r.defensiveness2_3 <- (selfinsight$r.defensiveness2_3-mean(selfinsight$r.defensiveness2_3,na.rm=TRUE))/sd(selfinsight$r.defensiveness2_3,na.rm=TRUE)
selfinsight$defensiveness <- with(selfinsight,(rowMeans(cbind(std.r.defensiveness1_1,std.r.defensiveness1_2,std.r.defensiveness1_3,std.defensiveness2_1,std.r.defensiveness2_2,std.r.defensiveness2_3), na.rm=TRUE)))
with(selfinsight, psych::alpha(data.frame(std.r.defensiveness1_1,std.r.defensiveness1_2,std.r.defensiveness1_3,std.defensiveness2_1,std.r.defensiveness2_2,std.r.defensiveness2_3), na.rm=TRUE, check.keys=FALSE))

selfinsight$attentiondeployment_1<- as.numeric(selfinsight$attentiondeployment_1); selfinsight$attentiondeployment_2<- as.numeric(selfinsight$attentiondeployment_2); selfinsight$attentiondeployment_3<- as.numeric(selfinsight$attentiondeployment_3)
selfinsight$distraction <- with(selfinsight,(rowMeans(cbind(attentiondeployment_1,attentiondeployment_2,attentiondeployment_3), na.rm=TRUE)))
with(selfinsight, psych::alpha(data.frame(attentiondeployment_1,attentiondeployment_2,attentiondeployment_3), na.rm=TRUE, check.keys=FALSE))

selfinsight$selfesteem_1<- as.numeric(selfinsight$selfesteem_1); selfinsight$selfesteem_2<- as.numeric(selfinsight$selfesteem_2); selfinsight$selfesteem_3<- as.numeric(selfinsight$selfesteem_3); selfinsight$selfesteem_4<- as.numeric(selfinsight$selfesteem_4); selfinsight$selfesteem_5<- as.numeric(selfinsight$selfesteem_5)
selfinsight$selfesteem_6<- as.numeric(selfinsight$selfesteem_6); selfinsight$selfesteem_7<- as.numeric(selfinsight$selfesteem_7); selfinsight$selfesteem_8<- as.numeric(selfinsight$selfesteem_8); selfinsight$selfesteem_9<- as.numeric(selfinsight$selfesteem_9); selfinsight$selfesteem_10<- as.numeric(selfinsight$selfesteem_10)
selfinsight$r.selfesteem_2 <- 5-selfinsight$selfesteem_2; selfinsight$r.selfesteem_5 <- 5-selfinsight$selfesteem_5; selfinsight$r.selfesteem_6 <- 5-selfinsight$selfesteem_6; selfinsight$r.selfesteem_8 <- 5-selfinsight$selfesteem_8; selfinsight$r.selfesteem_9 <- 5-selfinsight$selfesteem_9    
selfinsight$self.esteem <- with(selfinsight,(rowMeans(cbind(selfesteem_1,r.selfesteem_2,selfesteem_3,selfesteem_4,r.selfesteem_5,r.selfesteem_6,selfesteem_7,r.selfesteem_8,r.selfesteem_9,selfesteem_10), na.rm=TRUE)))
with(selfinsight, psych::alpha(data.frame(selfesteem_1,r.selfesteem_2,selfesteem_3,selfesteem_4,r.selfesteem_5,r.selfesteem_6,selfesteem_7,r.selfesteem_8,r.selfesteem_9,selfesteem_10), na.rm=TRUE, check.keys=FALSE))

#### SCORE TESTS OF ABILITIES ####

#### score the test of emotional abilities
#### response options:
#### Anger  (1) 
#### Disgust  (2) 
#### Fear  (3) 
#### Happiness  (4) 
#### Sadness  (5) 
#### Shame  (6) 
selfinsight$emorecogitem1 <- NA; selfinsight$emorecogitem1[selfinsight$eraitem1==6] <- 1; selfinsight$emorecogitem1[selfinsight$eraitem1!=6] <- 0
selfinsight$emorecogitem2 <- NA; selfinsight$emorecogitem2[selfinsight$eraitem2==3] <- 1; selfinsight$emorecogitem2[selfinsight$eraitem2!=3] <- 0
selfinsight$emorecogitem3 <- NA; selfinsight$emorecogitem3[selfinsight$eraitem3==2] <- 1; selfinsight$emorecogitem3[selfinsight$eraitem3!=2] <- 0
selfinsight$emorecogitem4 <- NA; selfinsight$emorecogitem4[selfinsight$eraitem4==6] <- 1; selfinsight$emorecogitem4[selfinsight$eraitem4!=6] <- 0
selfinsight$emorecogitem5 <- NA; selfinsight$emorecogitem5[selfinsight$eraitem5==1] <- 1; selfinsight$emorecogitem5[selfinsight$eraitem5!=1] <- 0
selfinsight$emorecogitem6 <- NA; selfinsight$emorecogitem6[selfinsight$eraitem6==4] <- 1; selfinsight$emorecogitem6[selfinsight$eraitem6!=4] <- 0
selfinsight$emorecogitem7 <- NA; selfinsight$emorecogitem7[selfinsight$eraitem7==6] <- 1; selfinsight$emorecogitem7[selfinsight$eraitem7!=6] <- 0
selfinsight$emorecogitem8 <- NA; selfinsight$emorecogitem8[selfinsight$eraitem8==1] <- 1; selfinsight$emorecogitem8[selfinsight$eraitem8!=1] <- 0
selfinsight$emorecogitem9 <- NA; selfinsight$emorecogitem9[selfinsight$eraitem9==6] <- 1; selfinsight$emorecogitem9[selfinsight$eraitem9!=6] <- 0
selfinsight$emorecogitem10 <- NA; selfinsight$emorecogitem10[selfinsight$eraitem10==5] <- 1; selfinsight$emorecogitem10[selfinsight$eraitem10!=5] <- 0
selfinsight$emorecogitem11 <- NA; selfinsight$emorecogitem11[selfinsight$eraitem11==4] <- 1; selfinsight$emorecogitem11[selfinsight$eraitem11!=4] <- 0
selfinsight$emorecogitem12 <- NA; selfinsight$emorecogitem12[selfinsight$eraitem12==3] <- 1; selfinsight$emorecogitem12[selfinsight$eraitem12!=3] <- 0
selfinsight$emorecogitem13 <- NA; selfinsight$emorecogitem13[selfinsight$eraitem13==4] <- 1; selfinsight$emorecogitem13[selfinsight$eraitem13!=4] <- 0
selfinsight$emorecogitem14 <- NA; selfinsight$emorecogitem14[selfinsight$eraitem14==1] <- 1; selfinsight$emorecogitem14[selfinsight$eraitem14!=1] <- 0
selfinsight$emorecogitem15 <- NA; selfinsight$emorecogitem15[selfinsight$eraitem15==3] <- 1; selfinsight$emorecogitem15[selfinsight$eraitem15!=3] <- 0
selfinsight$emorecogitem16 <- NA; selfinsight$emorecogitem16[selfinsight$eraitem16==1] <- 1; selfinsight$emorecogitem16[selfinsight$eraitem16!=1] <- 0
selfinsight$emorecogitem17 <- NA; selfinsight$emorecogitem17[selfinsight$eraitem17==2] <- 1; selfinsight$emorecogitem17[selfinsight$eraitem17!=2] <- 0
selfinsight$emorecogitem18 <- NA; selfinsight$emorecogitem18[selfinsight$eraitem18==5] <- 1; selfinsight$emorecogitem18[selfinsight$eraitem18!=5] <- 0
selfinsight$emorecogitem19 <- NA; selfinsight$emorecogitem19[selfinsight$eraitem19==1] <- 1; selfinsight$emorecogitem19[selfinsight$eraitem19!=1] <- 0
selfinsight$emorecogitem20 <- NA; selfinsight$emorecogitem20[selfinsight$eraitem20==5] <- 1; selfinsight$emorecogitem20[selfinsight$eraitem20!=5] <- 0
selfinsight$emorecogitem21 <- NA; selfinsight$emorecogitem21[selfinsight$eraitem21==4] <- 1; selfinsight$emorecogitem21[selfinsight$eraitem21!=4] <- 0
selfinsight$emorecogitem22 <- NA; selfinsight$emorecogitem22[selfinsight$eraitem22==5] <- 1; selfinsight$emorecogitem22[selfinsight$eraitem22!=5] <- 0
selfinsight$emorecogitem23 <- NA; selfinsight$emorecogitem23[selfinsight$eraitem23==3] <- 1; selfinsight$emorecogitem23[selfinsight$eraitem23!=3] <- 0
selfinsight$emorecogitem24 <- NA; selfinsight$emorecogitem24[selfinsight$eraitem24==4] <- 1; selfinsight$emorecogitem24[selfinsight$eraitem24!=4] <- 0
selfinsight$emorecogitem25 <- NA; selfinsight$emorecogitem25[selfinsight$eraitem25==3] <- 1; selfinsight$emorecogitem25[selfinsight$eraitem25!=3] <- 0
selfinsight$emorecogitem26 <- NA; selfinsight$emorecogitem26[selfinsight$eraitem26==5] <- 1; selfinsight$emorecogitem26[selfinsight$eraitem26!=5] <- 0
selfinsight$emorecogitem27 <- NA; selfinsight$emorecogitem27[selfinsight$eraitem27==4] <- 1; selfinsight$emorecogitem27[selfinsight$eraitem27!=4] <- 0
selfinsight$emorecogitem28 <- NA; selfinsight$emorecogitem28[selfinsight$eraitem28==1] <- 1; selfinsight$emorecogitem28[selfinsight$eraitem28!=1] <- 0
selfinsight$emorecogitem29 <- NA; selfinsight$emorecogitem29[selfinsight$eraitem29==6] <- 1; selfinsight$emorecogitem29[selfinsight$eraitem29!=6] <- 0
selfinsight$emorecogitem30 <- NA; selfinsight$emorecogitem30[selfinsight$eraitem30==1] <- 1; selfinsight$emorecogitem30[selfinsight$eraitem30!=1] <- 0
selfinsight$emorecogitem31 <- NA; selfinsight$emorecogitem31[selfinsight$eraitem31==3] <- 1; selfinsight$emorecogitem31[selfinsight$eraitem31!=3] <- 0
selfinsight$emorecogitem32 <- NA; selfinsight$emorecogitem32[selfinsight$eraitem32==2] <- 1; selfinsight$emorecogitem32[selfinsight$eraitem32!=2] <- 0
selfinsight$emorecogitem33 <- NA; selfinsight$emorecogitem33[selfinsight$eraitem33==4] <- 1; selfinsight$emorecogitem33[selfinsight$eraitem33!=4] <- 0
selfinsight$emorecogitem34 <- NA; selfinsight$emorecogitem34[selfinsight$eraitem34==2] <- 1; selfinsight$emorecogitem34[selfinsight$eraitem34!=2] <- 0
selfinsight$emorecogitem35 <- NA; selfinsight$emorecogitem35[selfinsight$eraitem35==5] <- 1; selfinsight$emorecogitem35[selfinsight$eraitem35!=5] <- 0
selfinsight$emorecogitem36 <- NA; selfinsight$emorecogitem36[selfinsight$eraitem36==6] <- 1; selfinsight$emorecogitem36[selfinsight$eraitem36!=6] <- 0
selfinsight$emorecogitem37 <- NA; selfinsight$emorecogitem37[selfinsight$eraitem37==2] <- 1; selfinsight$emorecogitem37[selfinsight$eraitem37!=2] <- 0
selfinsight$emorecogitem38 <- NA; selfinsight$emorecogitem38[selfinsight$eraitem38==4] <- 1; selfinsight$emorecogitem38[selfinsight$eraitem38!=4] <- 0
selfinsight$emorecogitem39 <- NA; selfinsight$emorecogitem39[selfinsight$eraitem39==4] <- 1; selfinsight$emorecogitem39[selfinsight$eraitem39!=4] <- 0
selfinsight$emorecogitem40 <- NA; selfinsight$emorecogitem40[selfinsight$eraitem40==5] <- 1; selfinsight$emorecogitem40[selfinsight$eraitem40!=5] <- 0
selfinsight$emorecogitem41 <- NA; selfinsight$emorecogitem41[selfinsight$eraitem41==2] <- 1; selfinsight$emorecogitem41[selfinsight$eraitem41!=2] <- 0
selfinsight$emorecogitem42 <- NA; selfinsight$emorecogitem42[selfinsight$eraitem42==6] <- 1; selfinsight$emorecogitem42[selfinsight$eraitem42!=6] <- 0
selfinsight$emorecogitem43 <- NA; selfinsight$emorecogitem43[selfinsight$eraitem43==3] <- 1; selfinsight$emorecogitem43[selfinsight$eraitem43!=3] <- 0
selfinsight$emorecogitem44 <- NA; selfinsight$emorecogitem44[selfinsight$eraitem44==5] <- 1; selfinsight$emorecogitem44[selfinsight$eraitem44!=5] <- 0
selfinsight$emorecogitem45 <- NA; selfinsight$emorecogitem45[selfinsight$eraitem45==2] <- 1; selfinsight$emorecogitem45[selfinsight$eraitem45!=2] <- 0
selfinsight$emorecogitem46 <- NA; selfinsight$emorecogitem46[selfinsight$eraitem46==6] <- 1; selfinsight$emorecogitem46[selfinsight$eraitem46!=6] <- 0
selfinsight$emorecogitem47 <- NA; selfinsight$emorecogitem47[selfinsight$eraitem47==5] <- 1; selfinsight$emorecogitem47[selfinsight$eraitem47!=5] <- 0
selfinsight$emorecogitem48 <- NA; selfinsight$emorecogitem48[selfinsight$eraitem48==1] <- 1; selfinsight$emorecogitem48[selfinsight$eraitem48!=1] <- 0
selfinsight$emorecogitem49 <- NA; selfinsight$emorecogitem49[selfinsight$eraitem49==3] <- 1; selfinsight$emorecogitem49[selfinsight$eraitem49!=3] <- 0
selfinsight$emorecogitem50 <- NA; selfinsight$emorecogitem50[selfinsight$eraitem50==1] <- 1; selfinsight$emorecogitem50[selfinsight$eraitem50!=1] <- 0
selfinsight$emorecogitem51 <- NA; selfinsight$emorecogitem51[selfinsight$eraitem51==5] <- 1; selfinsight$emorecogitem51[selfinsight$eraitem51!=5] <- 0
selfinsight$emorecogitem52 <- NA; selfinsight$emorecogitem52[selfinsight$eraitem52==1] <- 1; selfinsight$emorecogitem52[selfinsight$eraitem52!=1] <- 0
selfinsight$emorecogitem53 <- NA; selfinsight$emorecogitem53[selfinsight$eraitem53==2] <- 1; selfinsight$emorecogitem53[selfinsight$eraitem53!=2] <- 0
selfinsight$emorecogitem54 <- NA; selfinsight$emorecogitem54[selfinsight$eraitem54==6] <- 1; selfinsight$emorecogitem54[selfinsight$eraitem54!=6] <- 0
selfinsight$emorecogitem55 <- NA; selfinsight$emorecogitem55[selfinsight$eraitem55==1] <- 1; selfinsight$emorecogitem55[selfinsight$eraitem55!=1] <- 0
selfinsight$emorecogitem56 <- NA; selfinsight$emorecogitem56[selfinsight$eraitem56==2] <- 1; selfinsight$emorecogitem56[selfinsight$eraitem56!=2] <- 0
selfinsight$emorecogitem57 <- NA; selfinsight$emorecogitem57[selfinsight$eraitem57==5] <- 1; selfinsight$emorecogitem57[selfinsight$eraitem57!=5] <- 0
selfinsight$emorecogitem58 <- NA; selfinsight$emorecogitem58[selfinsight$eraitem58==5] <- 1; selfinsight$emorecogitem58[selfinsight$eraitem58!=5] <- 0
selfinsight$emorecogitem59 <- NA; selfinsight$emorecogitem59[selfinsight$eraitem59==4] <- 1; selfinsight$emorecogitem59[selfinsight$eraitem59!=4] <- 0
selfinsight$emorecogitem60 <- NA; selfinsight$emorecogitem60[selfinsight$eraitem60==2] <- 1; selfinsight$emorecogitem60[selfinsight$eraitem60!=2] <- 0
selfinsight$emorecogitem61 <- NA; selfinsight$emorecogitem61[selfinsight$eraitem61==3] <- 1; selfinsight$emorecogitem61[selfinsight$eraitem61!=3] <- 0
selfinsight$emorecogitem62 <- NA; selfinsight$emorecogitem62[selfinsight$eraitem62==6] <- 1; selfinsight$emorecogitem62[selfinsight$eraitem62!=6] <- 0
selfinsight$emorecogitem63 <- NA; selfinsight$emorecogitem63[selfinsight$eraitem63==2] <- 1; selfinsight$emorecogitem63[selfinsight$eraitem63!=2] <- 0
selfinsight$emorecogitem64 <- NA; selfinsight$emorecogitem64[selfinsight$eraitem64==1] <- 1; selfinsight$emorecogitem64[selfinsight$eraitem64!=1] <- 0
selfinsight$emorecogitem65 <- NA; selfinsight$emorecogitem65[selfinsight$eraitem65==3] <- 1; selfinsight$emorecogitem65[selfinsight$eraitem65!=3] <- 0
selfinsight$emorecogitem66 <- NA; selfinsight$emorecogitem66[selfinsight$eraitem66==4] <- 1; selfinsight$emorecogitem66[selfinsight$eraitem66!=4] <- 0
selfinsight$emorecogitem67 <- NA; selfinsight$emorecogitem67[selfinsight$eraitem67==6] <- 1; selfinsight$emorecogitem67[selfinsight$eraitem67!=6] <- 0
selfinsight$emorecogitem68 <- NA; selfinsight$emorecogitem68[selfinsight$eraitem68==6] <- 1; selfinsight$emorecogitem68[selfinsight$eraitem68!=6] <- 0
selfinsight$emorecogitem69 <- NA; selfinsight$emorecogitem69[selfinsight$eraitem69==4] <- 1; selfinsight$emorecogitem69[selfinsight$eraitem69!=4] <- 0
selfinsight$emorecogitem70 <- NA; selfinsight$emorecogitem70[selfinsight$eraitem70==3] <- 1; selfinsight$emorecogitem70[selfinsight$eraitem70!=3] <- 0
selfinsight$emorecogitem71 <- NA; selfinsight$emorecogitem71[selfinsight$eraitem71==2] <- 1; selfinsight$emorecogitem71[selfinsight$eraitem71!=2] <- 0
selfinsight$emorecogitem72 <- NA; selfinsight$emorecogitem72[selfinsight$eraitem72==3] <- 1; selfinsight$emorecogitem72[selfinsight$eraitem72!=3] <- 0
selfinsight$ERAtotal <- with(selfinsight, (rowSums(cbind(emorecogitem1,emorecogitem2,emorecogitem3,emorecogitem4,emorecogitem5,emorecogitem6,emorecogitem7,emorecogitem8,emorecogitem9,emorecogitem10,emorecogitem11,emorecogitem12,emorecogitem13,emorecogitem14,emorecogitem15,emorecogitem16,emorecogitem17,emorecogitem18,emorecogitem19,emorecogitem20,emorecogitem21,emorecogitem22,emorecogitem23,emorecogitem24,emorecogitem25,emorecogitem26,emorecogitem27,emorecogitem28,emorecogitem29,emorecogitem30,emorecogitem31,emorecogitem32,emorecogitem33,emorecogitem34,emorecogitem35,emorecogitem36,emorecogitem37,emorecogitem38,emorecogitem39,emorecogitem40,emorecogitem41,emorecogitem42,emorecogitem43,emorecogitem44,emorecogitem45,emorecogitem46,emorecogitem47,emorecogitem48,emorecogitem49,emorecogitem50,emorecogitem51,emorecogitem52,emorecogitem53,emorecogitem54,emorecogitem55,emorecogitem56,emorecogitem57,emorecogitem58,emorecogitem59,emorecogitem60,emorecogitem61,emorecogitem62,emorecogitem63,emorecogitem64,emorecogitem65,emorecogitem66,emorecogitem67,emorecogitem68,emorecogitem69,emorecogitem70,emorecogitem71,emorecogitem72), na.rm=TRUE)))
table(selfinsight$ERAtotal)
describe(selfinsight$ERAtotal)
selfinsight$ERApercent <- selfinsight$ERAtotal/72
#### calculate split half reliability of emotional abilities test
er.df <- with(selfinsight, data.frame(cbind(emorecogitem1,emorecogitem2,emorecogitem3,emorecogitem4,emorecogitem5,emorecogitem6,emorecogitem7,emorecogitem8,emorecogitem9,emorecogitem10,emorecogitem11,emorecogitem12,emorecogitem13,emorecogitem14,emorecogitem15,emorecogitem16,emorecogitem17,emorecogitem18,emorecogitem19,emorecogitem20,emorecogitem21,emorecogitem22,emorecogitem23,emorecogitem24,emorecogitem25,emorecogitem26,emorecogitem27,emorecogitem28,emorecogitem29,emorecogitem30,emorecogitem31,emorecogitem32,emorecogitem33,emorecogitem34,emorecogitem35,emorecogitem36,emorecogitem37,emorecogitem38,emorecogitem39,emorecogitem40,emorecogitem41,emorecogitem42,emorecogitem43,emorecogitem44,emorecogitem45,emorecogitem46,emorecogitem47,emorecogitem48,emorecogitem49,emorecogitem50,emorecogitem51,emorecogitem52,emorecogitem53,emorecogitem54,emorecogitem55,emorecogitem56,emorecogitem57,emorecogitem58,emorecogitem59,emorecogitem60,emorecogitem61,emorecogitem62,emorecogitem63,emorecogitem64,emorecogitem65,emorecogitem66,emorecogitem67,emorecogitem68,emorecogitem69,emorecogitem70,emorecogitem71,emorecogitem72)))
splithalf.r(er.df, sims = 1000, graph = TRUE)

#### score the test of cognitive abilities
selfinsight$ravenitem1 <- NA; selfinsight$ravenitem1[selfinsight$Raven1==7] <- 1; selfinsight$ravenitem1[selfinsight$Raven1!=7] <- 0
selfinsight$ravenitem2 <- NA; selfinsight$ravenitem2[selfinsight$Raven2==4] <- 1; selfinsight$ravenitem2[selfinsight$Raven2!=4] <- 0
selfinsight$ravenitem3 <- NA; selfinsight$ravenitem3[selfinsight$Raven3==6] <- 1; selfinsight$ravenitem3[selfinsight$Raven3!=6] <- 0
selfinsight$ravenitem4 <- NA; selfinsight$ravenitem4[selfinsight$Raven4==2] <- 1; selfinsight$ravenitem4[selfinsight$Raven4!=2] <- 0
selfinsight$ravenitem5 <- NA; selfinsight$ravenitem5[selfinsight$Raven5==4] <- 1; selfinsight$ravenitem5[selfinsight$Raven5!=4] <- 0
selfinsight$ravenitem6 <- NA; selfinsight$ravenitem6[selfinsight$Raven6==7] <- 1; selfinsight$ravenitem6[selfinsight$Raven6!=7] <- 0
selfinsight$ravenitem7 <- NA; selfinsight$ravenitem7[selfinsight$Raven7==3] <- 1; selfinsight$ravenitem7[selfinsight$Raven7!=3] <- 0
selfinsight$ravenitem8 <- NA; selfinsight$ravenitem8[selfinsight$Raven8==8] <- 1; selfinsight$ravenitem8[selfinsight$Raven8!=8] <- 0
selfinsight$ravenitem9 <- NA; selfinsight$ravenitem9[selfinsight$Raven9==7] <- 1; selfinsight$ravenitem9[selfinsight$Raven9!=7] <- 0
selfinsight$ravenitem10 <- NA; selfinsight$ravenitem10[selfinsight$Raven10==3] <- 1; selfinsight$ravenitem10[selfinsight$Raven10!=3] <- 0
selfinsight$ravenitem11 <- NA; selfinsight$ravenitem11[selfinsight$Raven11==7] <- 1; selfinsight$ravenitem11[selfinsight$Raven11!=7] <- 0
selfinsight$ravenitem12 <- NA; selfinsight$ravenitem12[selfinsight$Raven12==5] <- 1; selfinsight$ravenitem12[selfinsight$Raven12!=5] <- 0
selfinsight$ravenitem13 <- NA; selfinsight$ravenitem13[selfinsight$Raven13==5] <- 1; selfinsight$ravenitem13[selfinsight$Raven13!=5] <- 0
selfinsight$ravenitem14 <- NA; selfinsight$ravenitem14[selfinsight$Raven14==4] <- 1; selfinsight$ravenitem14[selfinsight$Raven14!=4] <- 0
selfinsight$ravenitem15 <- NA; selfinsight$ravenitem15[selfinsight$Raven15==1] <- 1; selfinsight$ravenitem15[selfinsight$Raven15!=1] <- 0
selfinsight$raventotal <- with(selfinsight, (rowSums(cbind(ravenitem1,ravenitem2,ravenitem3,ravenitem4,ravenitem5,ravenitem6,ravenitem7,ravenitem8,ravenitem9,ravenitem10,ravenitem11,ravenitem12,ravenitem13,ravenitem14,ravenitem15), na.rm=TRUE)))
selfinsight$ravenpercent <- selfinsight$raventotal/15
#### calculate split half reliability of cognitive abilities test
#### first create a data frame with valid cognitive ability data (as per the pre-registration) 
table(selfinsight$raventotalattempted)
selfinsight.valid.cognitive.ability <- subset(selfinsight, raventotalattempted>7 & ravenselfrating>-1)
table(selfinsight.valid.cognitive.ability$raventotal)
describe(selfinsight.valid.cognitive.ability$raventotal)
raven.rel <- with(selfinsight.valid.cognitive.ability, data.frame(cbind(ravenitem1,ravenitem2,ravenitem3,ravenitem4,ravenitem5,ravenitem6,ravenitem7,ravenitem8,ravenitem9,ravenitem10,ravenitem11,ravenitem12,ravenitem13,ravenitem14,ravenitem15)))
splithalf.r(raven.rel, sims = 1000, graph = TRUE)

# assign NAs to cognitive abilities test scores and self-views for those who do not meet criterion for inclusion in the main data frame

selfinsight$raventotal[selfinsight$raventotalattempted<8 | selfinsight$ravenselfrating<0] <- NA
selfinsight$ravenselfrating[selfinsight$raventotalattempted<8 | selfinsight$ravenselfrating<0] <- NA
with(selfinsight,describe(cbind(raventotal,ravenselfrating)))

#### EXAMINE EXISTENCE OF MATCHES AND MISMATCHES ####

### EMOTIONAL ABILITIES

### calculate standardized scores (z-scores) for each participant 
selfinsight$z.ERA <- (selfinsight$ERAtotal-36)/(sd(selfinsight$ERAtotal,na.rm=TRUE))
selfinsight$z.SV <- (selfinsight$ertselfrating-36)/(sd(selfinsight$ertselfrating,na.rm=TRUE))
with(selfinsight,describe(cbind(z.ERA, z.SV)))

### calculate the people who had accurate self-insight within half a SD
selfinsight$overestimater <- NA
selfinsight$overestimater[(selfinsight$z.ERA)<(selfinsight$z.SV-.5)] <- 1
selfinsight$overestimater[(selfinsight$z.ERA)>=(selfinsight$z.SV-.5)] <- 0
selfinsight$underestimater <- NA
selfinsight$underestimater[(selfinsight$z.ERA)>(selfinsight$z.SV+.5)] <- 1
selfinsight$underestimater[(selfinsight$z.ERA)<=(selfinsight$z.SV+.5)] <- 0
selfinsight$accurate <- NA; 
selfinsight$accurate[(selfinsight$z.ERA)>=(selfinsight$z.SV-.5) & (selfinsight$z.ERA)<=(selfinsight$z.SV+.5)] <- 1
selfinsight$accurate[(selfinsight$z.ERA)<(selfinsight$z.SV-.5) | (selfinsight$z.ERA)>(selfinsight$z.SV+.5)] <- 0

### display the people who were over- or under- or correct estimaters #
xtabs(~ overestimater, data = selfinsight)
xtabs(~ underestimater, data = selfinsight)
xtabs(~ accurate, data = selfinsight)

### COGNITIVE ABILITIES

### calculate standardized scores (z-scores) for each participant 
selfinsight.valid.cognitive.ability$z.raven <- (selfinsight.valid.cognitive.ability$raventotal-7.5)/(sd(selfinsight.valid.cognitive.ability$raventotal,na.rm=TRUE))
selfinsight.valid.cognitive.ability$z.ravenSV <- (selfinsight.valid.cognitive.ability$ravenselfrating-7.5)/(sd(selfinsight.valid.cognitive.ability$ravenselfrating,na.rm=TRUE))
with(selfinsight.valid.cognitive.ability,describe(cbind(z.raven, z.ravenSV)))

### calculate the people who had accurate self-insight within half a SD
selfinsight.valid.cognitive.ability$overestimater <- NA
selfinsight.valid.cognitive.ability$overestimater[(selfinsight.valid.cognitive.ability$z.raven)<(selfinsight.valid.cognitive.ability$z.ravenSV-.5)] <- 1
selfinsight.valid.cognitive.ability$overestimater[(selfinsight.valid.cognitive.ability$z.raven)>=(selfinsight.valid.cognitive.ability$z.ravenSV-.5)] <- 0
selfinsight.valid.cognitive.ability$underestimater <- NA
selfinsight.valid.cognitive.ability$underestimater[(selfinsight.valid.cognitive.ability$z.raven)>(selfinsight.valid.cognitive.ability$z.ravenSV+.5)] <- 1
selfinsight.valid.cognitive.ability$underestimater[(selfinsight.valid.cognitive.ability$z.raven)<=(selfinsight.valid.cognitive.ability$z.ravenSV+.5)] <- 0
selfinsight.valid.cognitive.ability$accurate <- NA; 
selfinsight.valid.cognitive.ability$accurate[(selfinsight.valid.cognitive.ability$z.raven)>=(selfinsight.valid.cognitive.ability$z.ravenSV-.5) & (selfinsight.valid.cognitive.ability$z.raven)<=(selfinsight.valid.cognitive.ability$z.ravenSV+.5)] <- 1
selfinsight.valid.cognitive.ability$accurate[(selfinsight.valid.cognitive.ability$z.raven)<(selfinsight.valid.cognitive.ability$z.ravenSV-.5) | (selfinsight.valid.cognitive.ability$z.raven)>(selfinsight.valid.cognitive.ability$z.ravenSV+.5)] <- 0

### display the people who were over- or under- or correct estimaters
xtabs(~ overestimater, data = selfinsight.valid.cognitive.ability)
xtabs(~ underestimater, data = selfinsight.valid.cognitive.ability)
xtabs(~ accurate, data = selfinsight.valid.cognitive.ability)

#### DESCRIPTIVES STATISTICS AND CORRELATIONS ####

#### emotional abilities test scores and self-views of emotional abilities
with(selfinsight, describe(cbind(ERAtotal,ERAtotalattempted,ertselfrating)))

#### correlate emotional abilities test scores with self-views of emotional abilities
with(selfinsight, cor.test(ERAtotal,ertselfrating), use="pairwise", adjust="none")

#### cognitive abilities test scores and self-views of cognitive abilities
with(selfinsight.valid.cognitive.ability, describe(cbind(raventotal,raventotalattempted,ravenselfrating)))

#### correlate cognitive abilities test scores with self-views of cognitive abilities
with(selfinsight.valid.cognitive.ability, cor.test(raventotal,ravenselfrating), use="pairwise", adjust="none")

#### correlate emotional and cognitive abilities test scores with self-views of emotional and cognitive abilities (note that this PAIRWISE and only includes observations with valid cognitive ability data as per the pre-registration)
with(selfinsight.valid.cognitive.ability, corr.test(cbind(ERAtotal,ERAtotalattempted,ertselfrating,raventotal,raventotalattempted,ravenselfrating)), use="pairwise", adjust="none")
with(selfinsight.valid.cognitive.ability, cor.test(ERAtotal,raventotal), use="pairwise", adjust="none")
with(selfinsight.valid.cognitive.ability, cor.test(ertselfrating,ravenselfrating), use="pairwise", adjust="none")

with(selfinsight, describe(cbind(raventotal,ravenselfrating,ERAtotal,ertselfrating,life.satisfaction,career.satisfaction,relationship.satisfaction,compliance.norms,adherence.guidance,defensiveness,distraction,self.esteem)))
with(selfinsight, corr.test(cbind(raventotal,ravenselfrating,ERAtotal,ertselfrating,life.satisfaction,career.satisfaction,relationship.satisfaction,compliance.norms,adherence.guidance,defensiveness,distraction, self.esteem)))

# repeat correlations to get exact p values
with(selfinsight, cor.test(raventotal,ravenselfrating))
with(selfinsight, cor.test(raventotal,life.satisfaction))
with(selfinsight, cor.test(raventotal,career.satisfaction))
with(selfinsight, cor.test(raventotal,relationship.satisfaction))
with(selfinsight, cor.test(raventotal,adherence.guidance))
with(selfinsight, cor.test(raventotal,compliance.norms))
with(selfinsight, cor.test(raventotal,defensiveness))
with(selfinsight, cor.test(raventotal,distraction))
with(selfinsight, cor.test(raventotal,self.esteem))

with(selfinsight, cor.test(ravenselfrating,life.satisfaction))
with(selfinsight, cor.test(ravenselfrating,career.satisfaction))
with(selfinsight, cor.test(ravenselfrating,relationship.satisfaction))
with(selfinsight, cor.test(ravenselfrating,adherence.guidance))
with(selfinsight, cor.test(ravenselfrating,compliance.norms))
with(selfinsight, cor.test(ravenselfrating,defensiveness))
with(selfinsight, cor.test(ravenselfrating,distraction))
with(selfinsight, cor.test(ravenselfrating,self.esteem))

with(selfinsight, cor.test(ERAtotal,ertselfrating))
with(selfinsight, cor.test(ERAtotal,raventotal))
with(selfinsight, cor.test(ERAtotal,ravenselfrating))
with(selfinsight, cor.test(ERAtotal,life.satisfaction))
with(selfinsight, cor.test(ERAtotal,career.satisfaction))
with(selfinsight, cor.test(ERAtotal,relationship.satisfaction))
with(selfinsight, cor.test(ERAtotal,adherence.guidance))
with(selfinsight, cor.test(ERAtotal,compliance.norms))
with(selfinsight, cor.test(ERAtotal,defensiveness))
with(selfinsight, cor.test(ERAtotal,distraction))
with(selfinsight, cor.test(ERAtotal,self.esteem))

with(selfinsight, cor.test(ertselfrating,raventotal))
with(selfinsight, cor.test(ertselfrating,ravenselfrating))
with(selfinsight, cor.test(ertselfrating,life.satisfaction))
with(selfinsight, cor.test(ertselfrating,career.satisfaction))
with(selfinsight, cor.test(ertselfrating,relationship.satisfaction))
with(selfinsight, cor.test(ertselfrating,adherence.guidance))
with(selfinsight, cor.test(ertselfrating,compliance.norms))
with(selfinsight, cor.test(ertselfrating,defensiveness))
with(selfinsight, cor.test(ertselfrating,distraction))
with(selfinsight, cor.test(ertselfrating,self.esteem))

with(selfinsight, cor.test(life.satisfaction,career.satisfaction))
with(selfinsight, cor.test(life.satisfaction,relationship.satisfaction))
with(selfinsight, cor.test(life.satisfaction,adherence.guidance))
with(selfinsight, cor.test(life.satisfaction,compliance.norms))
with(selfinsight, cor.test(life.satisfaction,defensiveness))
with(selfinsight, cor.test(life.satisfaction,distraction))
with(selfinsight, cor.test(life.satisfaction,self.esteem))

with(selfinsight, cor.test(career.satisfaction,relationship.satisfaction))
with(selfinsight, cor.test(career.satisfaction,adherence.guidance))
with(selfinsight, cor.test(career.satisfaction,compliance.norms))
with(selfinsight, cor.test(career.satisfaction,defensiveness))
with(selfinsight, cor.test(career.satisfaction,distraction))
with(selfinsight, cor.test(career.satisfaction,self.esteem))

with(selfinsight, cor.test(career.satisfaction,adherence.guidance))
with(selfinsight, cor.test(relationship.satisfaction,compliance.norms))
with(selfinsight, cor.test(relationship.satisfaction,defensiveness))
with(selfinsight, cor.test(relationship.satisfaction,distraction))
with(selfinsight, cor.test(relationship.satisfaction,self.esteem))

with(selfinsight, cor.test(adherence.guidance,compliance.norms))
with(selfinsight, cor.test(adherence.guidance,defensiveness))
with(selfinsight, cor.test(adherence.guidance,distraction))
with(selfinsight, cor.test(adherence.guidance,self.esteem))

with(selfinsight, cor.test(compliance.norms,defensiveness))
with(selfinsight, cor.test(compliance.norms,distraction))
with(selfinsight, cor.test(compliance.norms,self.esteem))

with(selfinsight, cor.test(defensiveness,distraction))
with(selfinsight, cor.test(defensiveness,self.esteem))

with(selfinsight, cor.test(distraction,self.esteem))

#### calculate reliabilities of adjustment measures

with(selfinsight, psych::alpha(data.frame(swls_1,swls_2,swls_3,swls_4,swls_5), na.rm=TRUE, check.keys=FALSE))
with(selfinsight, psych::alpha(data.frame(institutional_1,institutional_2,institutional_3,institutional_4,institutional_5), na.rm=TRUE, check.keys=FALSE))
with(selfinsight, psych::alpha(data.frame(interpersonal_1,interpersonal_2,r.interpersonal_3,r.interpersonal_4), na.rm=TRUE, check.keys=FALSE))

#### ANALYSES WITH COGNITIVE ABILITIES ####
#### (DATA FRAME IS SLIGHTLY SMALLER AS THREE PARTICIPANTS DROPPED OUT BEFORE THE COGNITIVE ABILITIES TEST)
#### code for condition-based regression analyses of self-enhancement effects is adapted from Humberg et al. 

#### CENTER VARIABLES

selfinsight.valid.cognitive.ability$c.raventotal <- selfinsight.valid.cognitive.ability$raventotal-7.5
selfinsight.valid.cognitive.ability$c.ravenselfrating <- selfinsight.valid.cognitive.ability$ravenselfrating-7.5
selfinsight.valid.cognitive.ability$c.raventotal2 <- selfinsight.valid.cognitive.ability$c.raventotal*selfinsight.valid.cognitive.ability$c.raventotal
selfinsight.valid.cognitive.ability$c.ravenselfrating2 <- selfinsight.valid.cognitive.ability$c.ravenselfrating*selfinsight.valid.cognitive.ability$c.ravenselfrating
selfinsight.valid.cognitive.ability$c.raventotal_c.ravenselfrating <- selfinsight.valid.cognitive.ability$c.ravenselfrating*selfinsight.valid.cognitive.ability$c.raventotal

with(selfinsight.valid.cognitive.ability, describe(cbind(life.satisfaction,career.satisfaction,relationship.satisfaction,c.raventotal,c.ravenselfrating)))

#### COGNITIVE ABLITIES AND LIFE SATISFACTION ####

## Specify the regression model, where the outcome is predicted by S and R
summary(model.cog.life <- lm(life.satisfaction ~ c.ravenselfrating + c.raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## examine if second-order terms explain additional variance
summary(model.cog.life.full.polynomial <- lm(life.satisfaction ~ c.raventotal + c.ravenselfrating + c.raventotal2 + c.raventotal_c.ravenselfrating + c.ravenselfrating2, data=selfinsight.valid.cognitive.ability, na.action=na.omit))
anova(model.cog.life, model.cog.life.full.polynomial)

## graph the relationship
RSA.cog.lifesat<-RSA(life.satisfaction~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.cog.lifesat,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(1,7),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Life Satisfaction")

# test self-enhancement
model <- 'life.satisfaction ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))
## Interpretation:
## abs is significantly bigger than zero
## therefore, there is an effect of self enhancement (SE)
## which is positive

#### COGNITIVE ABLITIES AND CAREER SATISFACTION ####

## Specify the regression model, where the outcome is predicted by S and R
summary(model.cog.career <- lm(career.satisfaction ~ c.ravenselfrating + c.raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## examine if second-order terms explain additional variance
summary(model.cog.career.full.polynomial <- lm(career.satisfaction ~ c.raventotal + c.ravenselfrating + c.raventotal2 + c.raventotal_c.ravenselfrating + c.ravenselfrating2, data=selfinsight.valid.cognitive.ability, na.action=na.omit))
anova(model.cog.career, model.cog.career.full.polynomial)

## graph the relationship
RSA.cog.careersat<-RSA(career.satisfaction~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.cog.careersat,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(1,5),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Career Satisfaction")

# test self-enhancement
model <- 'career.satisfaction ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))
## Interpretation:
## abs is significantly bigger than zero
## therefore, there is an effect of self enhancement (SE) (abs > 0) 
## this effect has the same sign as a3 := (c1 - c2). the sign is positive (c1 - c2 > 0)

#### COGNITIVE ABILITIES AND RELATIONSHIP SATISFACTION ####

## Specify the regression model, where the outcome is predicted by S and R
summary(model.cog.relationship <- lm(relationship.satisfaction ~ c.ravenselfrating + c.raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## examine if second-order terms explain additional variance
summary(model.cog.relationship.full.polynomial <- lm(relationship.satisfaction ~ c.raventotal + c.ravenselfrating + c.raventotal2 + c.raventotal_c.ravenselfrating + c.ravenselfrating2, data=selfinsight.valid.cognitive.ability, na.action=na.omit))
anova(model.cog.relationship, model.cog.relationship.full.polynomial)

## graph the relationship
RSA.cog.relationshipsat<-RSA(relationship.satisfaction~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.cog.relationshipsat,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(1,5),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Relationship Satisfaction")

# test self-enhancement
model <- 'relationship.satisfaction ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))
## Interpretation:
## abs is NOT significantly bigger than zero
## therefore, there is NO effect of self enhancement (SE)

#### ANALYSES OF EMOTIONAL ABILITES ####

#### CENTER VARIABLES

selfinsight$c.ERAtotal <- selfinsight$ERAtotal-36
selfinsight$c.ertselfrating <- selfinsight$ertselfrating-36
selfinsight$c.ERAtotal2 <- selfinsight$c.ERAtotal*selfinsight$c.ERAtotal
selfinsight$c.ertselfrating2 <- selfinsight$c.ertselfrating*selfinsight$c.ertselfrating
selfinsight$c.ERAtotal_c.ertselfrating <- selfinsight$c.ertselfrating*selfinsight$c.ERAtotal

#### ERA AND LIFE SATISFACTION ####

## Specify the regression model, where the outcome is predicted by S and R
summary(model.emo.life <- lm(life.satisfaction ~ c.ertselfrating + c.ERAtotal, data=selfinsight, na.action=na.omit))

## examine if second-order terms explain additional variance
summary(model.emo.life.full.polynomial <- lm(life.satisfaction ~ c.ERAtotal + c.ertselfrating + c.ERAtotal2 + c.ERAtotal_c.ertselfrating + c.ertselfrating2, data=selfinsight, na.action=na.omit))
anova(model.emo.life, model.emo.life.full.polynomial)

## graph the relationship
RSA.emotion.lifesat<-RSA(life.satisfaction~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.emotion.lifesat,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(1,7), xlab = "Emotional Ability",ylab = "Self View", zlab = "Life Satisfaction")

# test self-enhancement
model <- 'life.satisfaction ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))
## Interpretation:
## abs is significantly bigger than zero
## therefore, there is an effect of self enhancement (SE)

#### ERA AND CAREER SATISFACTION ####

## Specify the regression model, where the outcome is predicted by S and R
summary(model.emo.career <- lm(career.satisfaction ~ c.ertselfrating + c.ERAtotal, data=selfinsight, na.action=na.omit))

## examine if second-order terms explain additional variance
summary(model.emo.career.full.polynomial <- lm(career.satisfaction ~ c.ERAtotal + c.ertselfrating + c.ERAtotal2 + c.ERAtotal_c.ertselfrating + c.ertselfrating2, data=selfinsight, na.action=na.omit))
anova(model.emo.career, model.emo.career.full.polynomial)

## graph the relationship
RSA.emotion.careersat<-RSA(career.satisfaction~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.emotion.careersat,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(1,5), xlab = "Emotional Ability",ylab = "Self View", zlab = "Career Satisfaction")

# test self-enhancement
model <- 'career.satisfaction ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))
## Interpretation:
## abs is NOT significantly bigger than zero
## therefore, there is NO effect of self enhancement (SE)

#### ERA AND RELATIONSHIP SATISFACTION ####

## Specify the regression model, where the outcome is predicted by S and R
summary(model.emo.relationship <- lm(relationship.satisfaction ~ c.ertselfrating + c.ERAtotal, data=selfinsight, na.action=na.omit))

## examine if second-order terms explain additional variance
summary(model.emo.relationship.full.polynomial <- lm(relationship.satisfaction ~ c.ERAtotal + c.ertselfrating + c.ERAtotal2 + c.ERAtotal_c.ertselfrating + c.ertselfrating2, data=selfinsight, na.action=na.omit))
anova(model.emo.relationship, model.emo.relationship.full.polynomial)

## graph the relationship
RSA.emotion.relationshipsat<-RSA(relationship.satisfaction~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.emotion.relationshipsat,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(1,5), xlab = "Emotional Ability",ylab = "Self View", zlab = "Relationship Satisfaction")

# test self-enhancement
model <- 'relationship.satisfaction ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))
## Interpretation:
## abs is NOT significantly bigger than zero
## therefore, there is NO effect of self enhancement (SE)

#### CHECK CORRELATION BETWEEN EMOTION RECOGNITION ABILITY AND RELATIONSHIP SATISFACTION

with(selfinsight, cor.test(ERAtotal,relationship.satisfaction))

####
####
####
####

#### cognitive abilities and perceived compliance with norms ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.cog.compliance.norms <- lm(compliance.norms ~ ravenselfrating + raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## graph the relationship
RSA.compliance.cog<-RSA(compliance.norms~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.compliance.cog,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(1,7),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Compliance with Norms")

# test self-enhancement
model <- 'compliance.norms ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### cognitive abilities and perceived adherence to guidance ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.cog.adherence <- lm(adherence.guidance ~ ravenselfrating + raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## graph the relationship
RSA.adherence.cog<-RSA(adherence.guidance~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.adherence.cog,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(1,7),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Adherence to Guidance")

# test self-enhancement
model <- 'adherence.guidance ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c2-c1
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c2 - c1| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c2 - c1)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### cognitive abilities and defensiveness ####

describe(selfinsight.valid.cognitive.ability$defensiveness)

## Specify the regression model, where the outcome is predicted by R and S
summary(model.cog.defensivenss <- lm(defensiveness ~ ravenselfrating + raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## graph the relationship
RSA.defensiveness.cog<-RSA(defensiveness~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.defensiveness.cog,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(-3,3),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Defensiveness")

# test self-enhancement
model <- 'defensiveness ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c2-c1
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c2 - c1| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c2 - c1)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### cognitive abilities and distraction ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.cog.distraction <- lm(distraction ~ ravenselfrating + raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## graph the relationship
RSA.distraction.cog<-RSA(distraction~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.distraction.cog,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(1,7),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Distraction")

# test self-enhancement
model <- 'distraction ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c2-c1
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, se="robust.huber.white", data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c2 - c1| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c2 - c1)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### cognitive abilities and self-esteem ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.cog.selfesteem <- lm(self.esteem ~ ravenselfrating + raventotal, data=selfinsight.valid.cognitive.ability, na.action=na.omit))

## graph the relationship
RSA.selfesteem.cog<-RSA(self.esteem~c.raventotal*c.ravenselfrating,data=selfinsight.valid.cognitive.ability,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.selfesteem.cog,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-7.5,7.5),ylim = c(-7.5,7.5), zlim=c(1,4),xlab = "Cognitive Ability",ylab = "Self View", zlab = "Self Esteem")

# test self-enhancement
model <- 'self.esteem ~ 1 + c1*ravenselfrating + c2*raventotal
a1 := c1+c2
a3 := c2-c1
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model, data=selfinsight.valid.cognitive.ability)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c2 - c1| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c2 - c1)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

####
####
####
####

#### emotional abilities and perceived compliance with norms ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.emo.compliance.norms<- lm(compliance.norms ~ ertselfrating + ERAtotal, data=selfinsight, na.action=na.omit))

## graph the relationship
RSA.compliance.emo<-RSA(compliance.norms~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.compliance.emo,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(1,7), xlab = "Emotional Ability",ylab = "Self View", zlab = "Compliance with Norms")

# test self-enhancement
model.emo.compliance.norms <- 'compliance.norms ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model.emo.compliance.norms, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### emotional abilities and perceived adherence to guidance ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.emo.adherence.norms<- lm(adherence.guidance ~ ertselfrating + ERAtotal, data=selfinsight, na.action=na.omit))

## graph the relationship
RSA.adherence.emo<-RSA(adherence.guidance~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.adherence.emo,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(1,7), xlab = "Emotional Ability",ylab = "Self View", zlab = "Adherence to Guidance")

# test self-enhancement
model.emo.adherence.guidance <- 'adherence.guidance ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model.emo.adherence.guidance, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### emotional abilities and defensiveness ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.emo.defensiveness<- lm(defensiveness ~ ertselfrating + ERAtotal, data=selfinsight, na.action=na.omit))

## graph the relationship
RSA.defensiveness.emo<-RSA(defensiveness~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.defensiveness.emo,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(-3,3), xlab = "Emotional Ability",ylab = "Self View", zlab = "Defensiveness")

# test self-enhancement
model.emo.defensiveness <- 'defensiveness ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model.emo.defensiveness, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### emotional abilities and distraction ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.emo.distraction<- lm(distraction ~ ertselfrating + ERAtotal, data=selfinsight, na.action=na.omit))

## graph the relationship
RSA.distraction.emo<-RSA(distraction~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.distraction.emo,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(1,7), xlab = "Emotional Ability",ylab = "Self View", zlab = "Distraction")

# test self-enhancement
model.emo.distraction <- 'distraction ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model.emo.distraction, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

#### emotional abilities and self-esteem ####

## Specify the regression model, where the outcome is predicted by R and S
summary(model.emo.selfesteem <- lm(self.esteem ~ ertselfrating + ERAtotal, data=selfinsight, na.action=na.omit))

## graph the relationship
RSA.selfesteem.emo<-RSA(self.esteem~c.ERAtotal*c.ertselfrating,data=selfinsight,center=FALSE,scale=FALSE,na.rm=TRUE,out.rm=NULL,breakline=FALSE,models="default",cubic=FALSE,verbose=TRUE,estimator="MLR",se="robust",missing='listwise')
plot(RSA.selfesteem.emo,main = NULL,param = FALSE,coefs = FALSE,axes = c("LOC", "LOIC", "PA1", "PA2"),project = c("PA1", "PA2", "LOC"),xlim = c(-36,36),ylim = c(-36,36), zlim=c(1,4), xlab = "Emotional Ability",ylab = "Self View", zlab = "Self Esteem")

# test self-enhancement
model.emo.selfesteem <- 'self.esteem ~ 1 + c1*ertselfrating + c2*ERAtotal
a1 := c1+c2
a3 := c1-c2
c1_times_2 := 2*c1
c2_times_2 := 2*c2
minus_c1_times_2 := -2*c1
minus_c2_times_2 := -2*c2'
## Compute the regression
regression <- sem(model.emo.selfesteem, data=selfinsight)
## Table of estimated coefficients
estimates <- parameterEstimates(regression)
## Determination of the parameter abs := |c1 - c2| - |c1 + c2|,
## its standard error, respective p-value, and the sign of (c1 - c2)
## with the help of the lemma in Supplemental Material G:
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c2_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c2_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]>0){
  abs <- round(subset(estimates, label == "minus_c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "minus_c1_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "minus_c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "minus_c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]>=0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c1_times_2")["est"],3)
  se <- round(subset(estimates, label == "c1_times_2")["se"],3)
  a3_is <- "positive"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c1_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c1_times_2")["pvalue"]/2), 5)}
}
if (subset(estimates, label == "a3")["est"]<0 & subset(estimates, label == "a1")["est"]<0){
  abs <- round(subset(estimates, label == "c2_times_2")["est"],3)
  se <- round(subset(estimates, label == "c2_times_2")["se"],3)
  a3_is <- "negative"
  # compute one-tailed p-value of abs for the hypothesis abs > 0, 
  # depending on the tail in which abs is positioned
  if (abs >= 0){pvalue <- round(subset(estimates, label == "c2_times_2")["pvalue"]/2, 5)}
  if (abs < 0){pvalue <- round(1 - (subset(estimates, label == "c2_times_2")["pvalue"]/2), 5)}
}
print(c("abs"=abs,se,pvalue,"a3 is"=a3_is))

####
####
####
####

# examine correlations between potential mediators and adjustment measures -- emotional abilities data frame
with(selfinsight, corr.test(cbind(self.esteem,adherence.guidance,compliance.norms,defensiveness1,defensiveness2,distraction,life.satisfaction,career.satisfaction,relationship.satisfaction)))

# examine correlations between potential mediators and adjustment measures -- cognitive abilities data frame
with(selfinsight.valid.cognitive.ability, corr.test(cbind(self.esteem,adherence.guidance,compliance.norms,defensiveness1,defensiveness2,distraction,life.satisfaction,career.satisfaction,relationship.satisfaction)))
with(selfinsight.valid.cognitive.ability, cor.test(distraction,life.satisfaction))
with(selfinsight.valid.cognitive.ability, cor.test(distraction,career.satisfaction))
  
####
####
####
####
####

#### TEST THREE CANDIDATE MEDIATORS OF RELATIONSHIP BETWEEN SELF-ENHANCEMENT OF COGNITIVE ABILITIES AND LIFE SATISFACTION

#### test mediating effect of adherence.guidance on the relationship between self-enhancement on cognitive abilities and life satisfaction
#### first test whether negative association between cognitive abilities and life satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
adherence.guidance ~ a1*raventotal + ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and life satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
adherence.guidance ~ raventotal + a1*ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### test mediating effect of compliance.norms on the relationship between self-enhancement on cognitive abilities and life satisfaction
#### first test whether negative association between cognitive abilities and life satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
compliance.norms ~ a1*raventotal + ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*compliance.norms
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and life satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
compliance.norms ~ raventotal + a1*ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*compliance.norms
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### test mediating effect of distraction on the relationship between self-enhancement on cognitive abilities and life satisfaction
#### first test whether negative association between cognitive abilities and life satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
distraction ~ a1*raventotal + ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*distraction
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and life satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
compliance.norms ~ raventotal + a1*ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*compliance.norms
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### PIT THE TWO SUPPORTED MECHANISMS (adherence.guidance AND compliance.norms) AGAINST EACH OTHER

#### test mediating effect of both adherence.guidance and compliance.norms on the relationship between self-enhancement on cognitive abilities and life satisfaction
#### first test whether negative association between cognitive abilities and life satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
adherence.guidance ~ a1*raventotal + ravenselfrating
compliance.norms ~ a2*raventotal + ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance + b2*compliance.norms
# Calculate indirect effects
indirect.effect.1 := a1*b1
indirect.effect.2 := a2*b2
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and life satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
adherence.guidance ~ raventotal + a1*ravenselfrating
compliance.norms ~ raventotal + a2*ravenselfrating
life.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance + b2*compliance.norms
# Calculate indirect effect
indirect.effect.1 := a1*b1
indirect.effect.2 := a2*b2
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### BOOTSTRAP TEST THREE CANDIDATE MEDIATORS OF RELATIONSHIP BETWEEN SELF-ENHANCEMENT OF COGNITIVE ABILITIES AND CAREER SATISFACTION ####

#### test mediating effect of adherence.guidance on the relationship between self-enhancement on cognitive abilities and career satisfaction
#### first test whether negative association between cognitive abilities and life satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
adherence.guidance ~ a1*raventotal + ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and career satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
adherence.guidance ~ raventotal + a1*ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### bootstrap test mediating effect of compliance.norms on the relationship between self-enhancement on cognitive abilities and career satisfaction
#### first test whether negative association between cognitive abilities and career satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
compliance.norms ~ a1*raventotal + ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*compliance.norms
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and career satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
compliance.norms ~ raventotal + a1*ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*compliance.norms
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### test mediating effect of distraction on the relationship between self-enhancement on cognitive abilities and career satisfaction
#### first test whether negative association between cognitive abilities and career satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
distraction ~ a1*raventotal + ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*distraction
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and career satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
distraction ~ raventotal + a1*ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*distraction
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### PIT THE TWO SUPPORTED MECHANISMS (adherence.guidance AND compliance.norms) AGAINST EACH OTHER

#### test mediating effect of both adherence.guidance and compliance.norms on the relationship between self-enhancement on cognitive abilities and career satisfaction
#### first test whether negative association between cognitive abilities and career satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
adherence.guidance ~ a1*raventotal + ravenselfrating
compliance.norms ~ a2*raventotal + ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance + b2*compliance.norms
# Calculate indirect effects
indirect.effect.1 := a1*b1
indirect.effect.2 := a2*b2
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about cognitive abilities and career satisfaction (controlling for cognitive abilities) is mediated
model <- '
# Regression formulas
adherence.guidance ~ raventotal + a1*ravenselfrating
compliance.norms ~ raventotal + a2*ravenselfrating
career.satisfaction ~ raventotal + ravenselfrating + b1*adherence.guidance + b2*compliance.norms
# Calculate indirect effect
indirect.effect.1 := a1*b1
indirect.effect.2 := a2*b2
'
model.out <- lavaan(model, data=selfinsight.valid.cognitive.ability, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### BOOTSTRAP TESTS OF ONE CANDIDATE MEDIATOR OF RELATIONSHIP BETWEEN SELF-ENHANCEMENT OF EMOTIONAL ABILITIES AND LIFE SATISFACTION ####

#### test mediating effect of compliance.norms on the relationship between self-enhancement on emotional abilities and life satisfaction
#### first test whether negative association between emotional abilities and life satisfaction (controlling for self-views) is mediated
model <- '
# Regression formulas
compliance.norms ~ a1*ERAtotal + ertselfrating
life.satisfaction ~ ERAtotal + ertselfrating + b1*compliance.norms
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")
#### then test whether positive association between self-views about emotional abilities and life satisfaction (controlling for emotional abilities) is mediated
model <- '
# Regression formulas
compliance.norms ~ ERAtotal + a1*ertselfrating
life.satisfaction ~ ERAtotal + ertselfrating + b1*compliance.norms
# Calculate indirect effect
indirect.effect := a1*b1
'
model.out <- lavaan(model, data=selfinsight, auto.var=TRUE, se="boot", bootstrap = 1000)
summary(model.out, fit.measures=TRUE)
parameterEstimates(model.out, ci = TRUE, boot.ci.type = "perc")

#### END ####
